
package com.carsite.dao.hibernate;

import java.util.List;

import com.carsite.dao.hibernate.BaseDaoHibernate;
import com.carsite.model.Order;
import com.carsite.dao.OrderDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class OrderDaoHibernate extends BaseDaoHibernate implements OrderDao {

    /**
     * @see com.carsite.dao.OrderDao#getOrders(com.carsite.model.Order)
     */
    public List getOrders(final Order order) {
        return getHibernateTemplate().find("from Order");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.
        if (order == null) {
            return getHibernateTemplate().find("from Order");
        } else {
            // filter on properties set in the order
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(order).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(Order.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }*/
    }

    /**
     * @see com.carsite.dao.OrderDao#getOrder(Long orderId)
     */
    public Order getOrder(final Long orderId) {
        Order order = (Order) getHibernateTemplate().get(Order.class, orderId);
        if (order == null) {
            log.warn("uh oh, order with orderId '" + orderId + "' not found...");
            throw new ObjectRetrievalFailureException(Order.class, orderId);
        }

        return order;
    }

    /**
     * @see com.carsite.dao.OrderDao#saveOrder(Order order)
     */    
    public void saveOrder(final Order order) {
        getHibernateTemplate().saveOrUpdate(order);
    }

    /**
     * @see com.carsite.dao.OrderDao#removeOrder(Long orderId)
     */
    public void removeOrder(final Long orderId) {
        getHibernateTemplate().delete(getOrder(orderId));
    }

	public List getOrdersByOrderNo(String orderNo) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().find("from Order where orderNo=\'"+orderNo+"\'");
	}
    
    
    
}
